package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ShopCart;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ShopCartService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/shopCart")
@Slf4j
public class ShopCartController {

    @Resource
    private ShopCartService shopCartService;

    @GetMapping
    @LogScope(title = "购物车管理模块", content = "查看购物车列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的用户信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<ShopCart> userPageInfo = shopCartService.selectShopCartListByPage(current);
        return Result.success(userPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "购物车管理模块", content = "根据id查询购物车信息")
    public Result detail(@PathVariable String id){

        ShopCart shopCart = shopCartService.selectById(id);
        return Result.success(shopCart);
    }


    @DeleteMapping("/{id}")
    @LogScope(title = "购物车管理模块", content = "根据id删除购物车信息")
    public Result delete(@PathVariable String id){

        int i = shopCartService.deleteById(id);
        return i>0 ? Result.success() : Result.error();
    }

    @PutMapping
    @LogScope(title = "购物车管理模块", content = "编辑购物车信息")
    public Result edit(@RequestBody ShopCart shopCart){

        int i = shopCartService.edit(shopCart);
        return i>0 ? Result.success() : Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "购物车管理模块", content = "添加购物车信息")
    public Result save(@RequestBody ShopCart shopCart){

        shopCart.setId(RandomUtil.getRandomNumber());
        int i = shopCartService.save(shopCart);
        return i>0 ? Result.success() : Result.error();
    }

}
